﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TfL
{
    public interface ITrainTimesFeed : ITrackerNetFeed<TrainTimesForStation>
    {
    }

    public class TrainTimesFeed : ITrainTimesFeed
    {
        public TrainTimesFeed(string url, string ns , string line)
        {
            _feed = new TrackerNetFeed<TrainTimesForStation>(url,ns);
            _feed.DecodeResponse += DecodeResponse;
            _line = line;
            Url += string.Format("/PredictionSummary/{0}", LineCodes.FromName(line));
        }

        private void DecodeResponse(System.Net.WebResponse response)
        {
            //XmlReader reader = XmlReader.Create(response.GetResponseStream());
            
            //while(reader.Read())
            //{
            //    if(reader.Name == "ROOT")
            //    {
            //        TrainTimes times = new TrainTimes(_line);
            //        times.Parse(reader);
            //        foreach(var station in times.Stations)
            //            OnNext(station.Value);
            //    }
            //}
            TrainTimes times = new TrainTimes(_line);
            foreach(var station in times.Parse(response.GetResponseStream()))
            {
                
                OnNext(station);
            }

        }

        public override string ToString()
        {
            return string.Format("{0}",_line);
        }

        #region ITrackerNetFeed<TrainTimesForStation> Members

        public string Ns
        {
            get { return _feed.Ns; }
        }

        public string Url
        {
            get
            {
                return _feed.Url;
            }
            set
            {
                _feed.Url = value;
            }
        }

        #endregion

        #region IObserver<TrainTimesForStation> Members

        public void OnCompleted()
        {
            _feed.OnCompleted();
        }

        public void OnError(Exception error)
        {
            _feed.OnError(error);
        }

        public void OnNext(TrainTimesForStation value)
        {
            _feed.OnNext(value);
        }

        #endregion

        #region IObservable<TrainTimesForStation> Members

        public IDisposable Subscribe(IObserver<TrainTimesForStation> observer)
        {
            return _feed.Subscribe(observer);
        }

        #endregion

        #region ITrackerNetFeedPoller<TrainTimesForStation> Members

        public System.Reactive.Subjects.ISubject<TrainTimesForStation> Poll()
        {
            return _feed.Poll();
        }

        public System.Reactive.Subjects.ISubject<TrainTimesForStation> Poll(System.Threading.CancellationToken token, int period)
        {
            return _feed.Poll(token,period);
        }

        #endregion

        private readonly string _line = string.Empty;

        private readonly TrackerNetFeed<TrainTimesForStation> _feed;
    }
}
